import os
import sys
import math
import numpy as np
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class DrawTab(QTabWidget):
    choose_attr_Signal=pyqtSignal(dict)
    def __init__(self):
        super(DrawTab, self).__init__()
        self.IMG_PATH=os.path.join(os.path.dirname(os.path.dirname(__file__)),'imgs')
        self.BLANK_IMG=os.path.join(self.IMG_PATH,'0.png')
        self.COLUMN=4
        self.btn_list=[] #所有的按钮
        self.sky_btn_list=[] #天空按钮列表
        self.ground_btn_list=[]  #地面按钮列表
        self.water_btn_list=[]  #水类按钮列表
        self.animal_btn_list=[]  #动物按钮列表
        self.plant_btn_list=[]  #植物按钮列表
        self.style_btn_list=[]  #风格按钮列表

        # self.main_layout=QVBoxLayout()
        self.sky_widget=QWidget()
        self.sky_widget_layout=QGridLayout()
        # self.init_sky()
        self.init_img('sky',self.sky_widget_layout)
        self.sky_widget.setLayout(self.sky_widget_layout)
        self.addTab(self.sky_widget, '天空')

        self.terrain_widget=QWidget() 
        self.terrain_widget_layout=QGridLayout()
        # self.init_ground()
        self.init_img('terrain', self.terrain_widget_layout)
        self.terrain_widget.setLayout(self.terrain_widget_layout)
        self.addTab(self.terrain_widget, '地面')

        self.water_widget=QWidget()
        self.water_widget_layout=QGridLayout()
        # self.init_water()
        self.water_widget.setLayout(self.water_widget_layout)
        self.addTab(self.water_widget,'江海')

        self.animal_widget=QWidget()
        self.animal_widget_layout=QGridLayout()
        # self.init_animal()
        self.animal_widget.setLayout(self.animal_widget_layout)
        self.addTab(self.animal_widget,'动物')

        self.building_widget=QWidget()
        self.building_widget_layout=QGridLayout()
        # self.init_ground()
        self.init_img('building', self.building_widget_layout)
        self.building_widget.setLayout(self.building_widget_layout)
        self.addTab(self.building_widget, '建筑')

        self.traffic_widget=QWidget()
        self.traffic_widget_layout=QGridLayout()
        # self.init_ground()
        self.traffic_widget.setLayout(self.traffic_widget_layout)
        self.addTab(self.traffic_widget, '交通')


        self.plant_widget=QWidget()
        self.plant_widget_layout=QGridLayout()
        # self.init_plant()
        self.plant_widget.setLayout(self.plant_widget_layout)
        self.addTab(self.plant_widget,'植物')

        self.style_widget=QWidget()
        self.style_widget_layout=QGridLayout()
        # self.init_style()
        self.style_widget.setLayout(self.style_widget_layout)
        self.addTab(self.style_widget,'风格')

        # self.setLayout()

    def convert_array(self,a_list, COL):
        """
        根据图片数量生成numpy数组形式
        :param a_list:
        :param COL:
        :return:
        """
        ROW = math.ceil(len(a_list) / COL)
        ALL = ROW * COL
        delta = ALL - len(a_list)
        a_list.extend([self.BLANK_IMG] * delta)
        na = np.array(a_list)
        b = na.reshape(ROW, COL)
        return b,ALL

    def add_grid_toolbtn(self,layout,np_img,tool_btn_list):
        """
        对给的的layout添加toolbtn
        :param layout:
        :param np_img:
        :param tool_btn_list:
        :return:
        """
        shape=np_img.shape
        row=shape[0]
        column=shape[1]
        index=0
        for r in range(row):
            for c in range(column):
                layout.addWidget(tool_btn_list[index],r,c)
                index+=1

    def btn_style(self,selected):
        """
        设置按钮样式，当选择时，背景变黄，不被选中时，回复
        :param selected:
        :return:
        """
        if selected==True:
            return 'background-color: #87CEFA'
        else:
            return 'background-color: #D3D3D3'

    def init_img(self,img_tpye,widget_layout):
        """
        通用生成img按钮
        :param img_tpye:
        :return:
        """

        path = os.path.join(self.IMG_PATH, img_tpye)
        print('path:',path)
        file_list=os.listdir(path)
        imgs=[i for i in file_list if i.endswith('.png')]
        # imgs = os.listdir(path)   #这里是演示用的读取本地图片信息，其实是应该读取GC库里的路径信息
        imgs_path_list = []
        tool_btn_list = []
        if imgs:
            for i in imgs:
                # if i.endswith('.png')
                img_path = os.path.join(path, i)
                imgs_path_list.append(img_path)  #
                tool_btn = QToolButton()
                tool_btn.setText(img_path)
                tool_btn.setStyleSheet(self.btn_style(False))
                tool_btn.setToolTip(i)
                tool_btn.clicked.connect(lambda:self._btn_click(img_tpye))
                icon = QIcon()
                icon_path = img_path
                icon.addPixmap(QPixmap(icon_path))
                tool_btn.setIcon(icon)
                tool_btn.setIconSize(QSize(150, 150))
                tool_btn_list.append(tool_btn)

            np_img, counts = self.convert_array(imgs_path_list, self.COLUMN)

            for c in range(counts - len(imgs)):
                img_path = os.path.join(self.IMG_PATH, '0.png')
                imgs_path_list.append(img_path)
                tool_btn = QToolButton()
                tool_btn.setText('blank')
                tool_btn.setToolTip('blank')
                icon = QIcon()
                icon_path = img_path
                icon.addPixmap(QPixmap(icon_path))
                tool_btn.setIcon(icon)
                tool_btn.setIconSize(QSize(150, 150))
                tool_btn_list.append(tool_btn)
            self.add_grid_toolbtn(widget_layout, np_img, tool_btn_list)
        self.btn_list.extend(tool_btn_list)


    # def init_sky(self):
    #     """
    #     所有图片名称就是CG库里的唯一标识符
    #     :return:
    #     """
    #     sky_path=os.path.join(self.IMG_PATH,'sky')
    #     imgs=os.listdir(sky_path)
    #     imgs_path_list=[]
    #     tool_btn_list=[]
    #     if imgs:
    #         for i in imgs:
    #             img_path=os.path.join(sky_path,i)
    #             # print('img_path:',img_path)
    #             imgs_path_list.append(img_path)
    #             tool_btn=QToolButton()
    #             tool_btn.setText(img_path)
    #             tool_btn.setStyleSheet(self.btn_style(False))
    #             tool_btn.setToolTip(i)
    #             tool_btn.clicked.connect(self.sky_btn_click)
    #             icon=QIcon()
    #             icon_path = img_path
    #             icon.addPixmap(QPixmap(icon_path))
    #             tool_btn.setIcon(icon)
    #             tool_btn.setIconSize(QSize(150, 150))
    #             tool_btn_list.append(tool_btn)
    #
    #         np_img,counts=self.convert_array(imgs_path_list,self.COLUMN)
    #
    #         for c in range(counts-len(imgs)):
    #             img_path=os.path.join(self.IMG_PATH,'0.png')
    #             imgs_path_list.append(img_path)
    #             tool_btn=QToolButton()
    #             tool_btn.setText('blank')
    #             tool_btn.setToolTip('blank')
    #             icon=QIcon()
    #             icon_path = img_path
    #             icon.addPixmap(QPixmap(icon_path))
    #             tool_btn.setIcon(icon)
    #             tool_btn.setIconSize(QSize(150, 150))
    #             tool_btn_list.append(tool_btn)
    #         self.add_grid_toolbtn(self.sky_widget_layout,np_img,tool_btn_list)
    #     self.sky_btn_list=tool_btn_list

    def _btn_click(self,img_type):
        btn=self.sender()
        print('img_type:',img_type)
        btn.setStyleSheet(self.btn_style(True))
        for i in self.btn_list:
            if i != btn:
                i.setStyleSheet(self.btn_style(False))
        attr={'path':btn.text(),'img_type':img_type}
        self.choose_attr_Signal.emit(attr)

    def sky_btn_click(self):
        btn=self.sender()
        btn.setStyleSheet(self.btn_style(True))
        for i in self.sky_btn_list:
            if i != btn:
                i.setStyleSheet(self.btn_style(False))
        attr = {'path': btn.text()}
        self.choose_attr_Signal.emit(attr)